/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* includes (local) ----------------------------------------------------------*/
#include "m_memtry.h"
/* includes (standard library, system) ---------------------------------------*/
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
#include <x_warn.h>

#ifdef CONFIG_SYSCALL
#include <x_syscall.h>
#endif

#include <k_errno.h>

#include <a_if.h>

/* defines -------------------------------------------------------------------*/
/* typedefs ------------------------------------------------------------------*/
/* macro ---------------------------------------------------------------------*/
/* variables (global) --------------------------------------------------------*/
/* variables (extern) --------------------------------------------------------*/
/* variables (local) ---------------------------------------------------------*/
/* inlines -------------------------------------------------------------------*/
/* functions -----------------------------------------------------------------*/

/**
 * mem_try - try to copy from <src> to <dst> 1 (1 << <order>) bytes unit
 * @dst:   the address will try write to
 * @src:   the address will try read from
 * @order: try to copy (1 << order) bytes
 *
 * return: 0 on success, negative value on error
 */
int mem_try(void *dst, void *src, int order)
{
    WARN_ON(order > MEMTRY_MAX_ORDER, errno = ERRNO_MEMTRY_ILLEGAL_ORDER;
            return -1, "Invalid order!");

    if (mem_try_arch(dst, src, order) != 0) {
        errno = ERRNO_MEMTRY_NO_ACCESS;
        return -1;
    }

    return 0;
}

#ifdef CONFIG_SYSCALL
static const uintptr_t syscall_entries_memtry[] = {
    (uintptr_t)mem_try,
};

const struct syscall_table syscall_table_memtry = {
    ARRAY_SIZE(syscall_entries_memtry),
    syscall_entries_memtry,
};
#endif
